package com.kt.controller;

import com.alibaba.fastjson.JSON;
import com.kt.domain.Order;
import com.kt.domain.Product;
import com.kt.domain.User;
import com.kt.service.imp.OrderServiceImp;
import com.kt.service.imp.ProductServiceImp;
import com.kt.service.imp.UserServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/userCenter.do")
public class UserCenterServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        UserServiceImp userServiceImp = new UserServiceImp();
        String username = req.getParameter("username");

        User user = userServiceImp.findByUsername(username);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("user", user);
        if (user.getLevel() == 1) {
            // 买家
            OrderServiceImp orderServiceImp = new OrderServiceImp();
            List<Order> list = orderServiceImp.findByUid(user.getUid()+"");
            map.put("orders", list);
        } else {
            // 卖家
            ProductServiceImp productServiceImp = new ProductServiceImp();
            List<Product> list = productServiceImp.findProductByUid(user.getUid());
            map.put("products", list);
        }
        String userInfo = JSON.toJSONString(map);
        resp.getWriter().write(userInfo);


    }
}
